/*
 * @Descripttion: 
 * @version: 
 * @Author: cyj
 * @Date: 2023-01-07 00:04:59
 */

//判断手机号
export function isPhone(s) {
	var reg = /^1[356789]\d{9}$/;
	if (!reg.test(s)) {
		return false;
	}
	return true;
}

//判断密码
export function isPwd(s) {
	let reg = /(\d\w)|(\W\d)/  //匹配数字和单词、非单词
	if (!reg.test(s)) {
		return false
	}
	return true
}
/**
 * 
 localStorage存取数据
 */
export const setItem = (key, value) => {
	if (typeof value === "object") {
		value = JSON.stringify(value);
	}
	window.localStorage.setItem(key, value);
};
export const getItem = key => {
	const data = window.localStorage.getItem(key);
	try {
		return JSON.parse(data);
	} catch (err) {
		return data;
	}
};
export const removeItem = key => {
	window.localStorage.removeItem(key);
};

//防抖操作
export const debounce = (fn, delay, immediate = false) => {
	let timer; //借助闭包 [timer一直存在且一直+1]
	let isInvoke = false; //用于判断是否立即执行
	return function () {
		let context = this;
		let args = arguments;
		if (timer) clearTimeout(timer);  //清除timer 
		if (immediate && !isInvoke) {  //立即执行
			fn.apply(context, args);
			isInvoke = true
			// console.log('刚进入的timer',timer);
			// let callNow = !timer;
			// timer = setTimeout(() => {
			//     timer = null
			//     console.log(timer);
			// }, delay)

			// console.log('callNow',callNow);
			// if (callNow) fn.apply(context, args);   //完善this指向和event指向

		} else {  //延迟执行
			timer = setTimeout(function () {
				fn.apply(context, args);//完善this指向和event指向
				isInvoke = false
			}, delay);
		}
	};
}

//节流操作  第一次默认执行
export const throttle = (fn, delay, immediate = true) => {
	let startTime = 0
	return function () {
		let nowTime = new Date().getTime()  //获取当前时间戳

		//对立即执行控制 不希望第一次执行
		if (!immediate && startTime === 0) {
			startTime = nowTime
		}

		//因为nowTime一开始太大，waitTIme肯定为负，所以默认第一次执行
		let waitTIme = delay - (nowTime - startTime)  //获取等待触发的时间  
		if (waitTIme <= 0) {
			fn()
			startTime = nowTime
		}
	}
}


//将图片转为base64
// export const gotoBase64 = (url) => {
// 	let canvas = document.createElement("canvas");
// 	let ctx = canvas.getContext("2d");
// 	let img = new Image()
// 	let url = ''
// 	img.crossOrigin = 'anonymous'
// 	img.onload = function () {
// 		canvas.width = img.width;
// 		canvas.height = img.height;
// 		ctx.drawImage(img, 0, 0, img.width, img.height);
// 		let dataURL = canvas.toDataURL("image/png");
// 		return dataURL;
// 	}
// 	img.src = url
// }
//#region 
// function image2Base64(img) {
//     var canvas = document.createElement("canvas");
//     canvas.width = img.width;
//     canvas.height = img.height;
//     var ctx = canvas.getContext("2d");
//     ctx.drawImage(img, 0, 0, img.width, img.height);
//     var dataURL = canvas.toDataURL("image/png");
//     return dataURL;
// }

// function getImgBase64(){
//     var base64="";
//     var img = new Image();
//     img.src="img/test.jpg";
//     img.onload=function(){
//         base64 = image2Base64(img);
//         alert(base64);
//     }
// }
// getImgBase64();
//#endregion

//#region 
// function convertImgToBase64(url, callback, outputFormat){
//     var canvas = document.createElement('CANVAS'),
//         ctx = canvas.getContext('2d'),
//         img = new Image;
//     img.crossOrigin = 'Anonymous';
//     img.onload = function(){
//         canvas.height = img.height;
//         canvas.width = img.width;
//         ctx.drawImage(img,0,0);
//         var dataURL = canvas.toDataURL(outputFormat || 'image/png');
//         callback.call(this, dataURL);
//         canvas = null; 
//     };
//     img.src = url;
// }

// var url = "static/img/js1.jpg";//这是站内的一张图片资源，采用的相对路径
// convertImgToBase64(url, function(base64Img){
//     //转化后的base64
//     alert(base64Img);
// });
//#endregion